package cn.hongsw.quartz.controller;

import cn.hongsw.quartz.job.PrintWordsJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :hongsw
 * @date :2021-08-09 10:54
 */
@RestController
public class JobController {
    @Autowired
    private Scheduler scheduler;

    @GetMapping("start")
    public String start() throws SchedulerException {
        scheduler.start();
        return "scheduler start !";
    }


    @GetMapping("add")
    public String add() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(PrintWordsJob.class)
                .withIdentity("a", "b").build();
        jobDetail.getJobDataMap().put("param", "这是传值方法");
        CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule("* * * * * ?");
        // 3、构建Trigger实例,每隔1s执行一次
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("c", "d")
                .withSchedule(builder).build();
        //4、执行
        scheduler.scheduleJob(jobDetail, trigger);
        return "add successful !";
    }

    @GetMapping("stop")
    public String stop() throws SchedulerException {
        scheduler.deleteJob(new JobKey("a", "b"));
        return "stop successful !";
    }


}
